<?php

include_once ('configure.php');
include_once ('class.fileLog.php');
class Db
{
  
  var $link;
  var $str_err;
  
  /** Costrutture della classe
	* @param void.
	*/
  function Db()
  {
	 $str_err="";
	 $link="";
  }
  
  /** Connessione DB nome Hostname, user, password
  	* sono configurate nel file configure.php
	* @param void.
	*/
  function connect() 
  {
	$connect      = mysql_connect(MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD);
	$select_db    = mysql_select_db (MYSQL_DATABASE, $connect); 
	if (!$connect) {
	   $errno  = mysql_errno();
	   switch($errno) {
		  case 1045 : { $this->error(__FILE__.":".__LINE__." "); break; }
		}
	}
	$this->link=$connect;
   }
   
   /** Chiusara connessione DB
	* @param void.
	*/
   function disconnettiDB()
   {
		mysql_close($this->link);
   }
   
   /** Gestione error salvati nel log_file 
   	* classe class.fileLog.php
	* @param error_text
	*/
   function error($text) {
	   $log=new FileLog();
	   $log->writeLog("class.db.php function connect(): MYSQL SERVER ERROR :".$text);
	   $log->close();
   }
   
	/** funzione execute().\n
	* Viene usata per INSERT, UPDATE, DELETE...
	* @param $query.
	* @param ritorna resource 
	*/
	function execute($query)
    {
		$result=mysql_query($query,$this->link) or $this->error(__FILE__.":".__LINE__." ".$query);
		return $result;
    }
	
	/** funzione execute_select().\n
	* Viene usata per SELECT
	* @param $query.
	* @param ritorna matrice[righe][colonne]
	*/
	function execute_select($query)
    {
		$point=$this->execute($query);
                $result=array();
                while($tmp = mysql_fetch_row($point))
		{
			$result[]=$tmp;
		} 
                mysql_free_result($point);
                $result=array_reverse($result);
		return $result;
    }
    
    /** funzione execute_select().\n
	* Viene usata per SELECT
	* @param $query.
	* @param ritorna matrice[righe][colonne]
	*/
	function execute_select_array($query)
    {
		$point=$this->execute($query);
                $result=array();
                while($tmp = mysql_fetch_row($point))
		{
			$result[$tmp[0]]=$tmp[0];
		} 
                mysql_free_result($point);
		return $result;
    }
 }  
?>  